﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System.Data.Common;
using System.Data;
using System.Collections;

namespace AMStudio.Common
{
    internal class ItacTagDAL
    {
        //private DataSet myDataset;

        public ItacTagDAL()
        {
            //myDataset = new DataSet();
            //Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MachineConfig");
            //string sql = "select * from [users$]";
            //DbCommand cmd = db.GetSqlStringCommand(sql);

            //string productsTable = "[users$]";

            //// Retrieve the initial data.
            //db.LoadDataSet(cmd, myDataset, productsTable);

            //// Get the table that will be modified.
            //DataTable dTable = myDataset.Tables[productsTable];

            

        }
        public bool Add(int nestID, string material)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            StringBuilder sb = new StringBuilder();
            sb.Append(
                "insert into ITAC values(@NESTID,@MATERIAL)");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());

            db.AddInParameter(cmd, "@NESTID", DbType.Int32, nestID);
            db.AddInParameter(cmd, "@MATERIAL", DbType.String, material);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        public bool Delete(int nestID)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from ITAC ");
            sb.Append(" where NESTID=@NESTID");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@NESTID", DbType.Int32, nestID);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public List<string> SelectByNestID( int nestId)
        {
            var list = new List<string>();
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            string sql = "select * from [ITAC] where NESTID="+nestId.ToString();
            DataSet ds = db.ExecuteDataSet(CommandType.Text, sql);
            DataTable dt = ds.Tables[0];
            if (dt != null)
            {
                foreach (DataRow item in dt.Rows)
                {
                    list.Add(item[1].ToString());
                }
            }
            //DataAccessor<User> myAccessor;
            //myAccessor = db.CreateSqlStringAccessor("select * from [ITAC] where NESTID=",
            //    MapBuilder<User>.MapAllProperties().
            //    Build()
            //    );
            //list = myAccessor.Execute()
            //    .ToDictionary<User,string>
            //    ((item)=>
            //        {
            //            return item.UserName.Trim();
            //        });
            return list;
        }
    }
}
